package com.iiifi.kite.metrics.undertow;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.undertow.Undertow;


/**
 * Undertow Metrics 配置
 *
 * @author kite@iiifi.com 花朝
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(Undertow.class)
@AutoConfigureBefore(ServletWebServerFactoryAutoConfiguration.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class UndertowMetricsConfiguration {

    @Bean
    public UndertowMetrics undertowMetrics() {
        return new UndertowMetrics();
    }

    @Bean
    public WebServerFactoryCustomizer<UndertowServletWebServerFactory> undertowMetricsWebServerFactoryCustomizer(
            UndertowMetrics undertowMetrics) {
        return factory -> factory
                .addDeploymentInfoCustomizers(customizers -> customizers.setMetricsCollector(undertowMetrics));
    }

}
